﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 可视化工具.View.Csv文件
{
    public partial class CsvModelForm : Form
    {
        public string beginText = string.Empty;
        private CsvMainForm csvMainForm;

        public CsvModelForm()
        {
            InitializeComponent();
        }

        public CsvModelForm(CsvMainForm csvMainForm) : this()
        {
            this.csvMainForm = csvMainForm;
        }

        /// <summary>
        /// 更改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void button1_Click(object sender, EventArgs e)
        {
            try
            {
                button1.Enabled = false;
                textBox模板.Enabled = true;
                button2.Enabled = true;
                beginText = textBox模板.Text.Trim();
            }
            catch
            {

            }
        }

        /// <summary>
        /// 确认按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public async void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if(label1.Text + textBox模板.Text.Trim()== label1.Text + beginText)
                {
                    beginText = string.Empty;
                    button2.Enabled = false;
                    textBox模板.Enabled = false;
                    button1.Enabled = true;
                    return;
                }

                if (textBox模板.Text.Trim() == "")
                {
                    MessageBox.Show("名称不能为空", "错误");
                    textBox模板.Text = beginText.Trim();
                }
                else
                {
                    //更名成功
                    if (this.Parent is TabPage page)
                    {
                        page.Text = label1.Text + textBox模板.Text.Trim();
                        page.Name = label1.Text + textBox模板.Text.Trim();
                        if (csvMainForm.csvConfig.modelName.Contains("_@#"))
                        {
                            csvMainForm.csvConfig.modelName = label1.Text + textBox模板.Text + "_@#";
                        }
                        else
                        {
                            csvMainForm.csvConfig.modelName = label1.Text + textBox模板.Text;
                            try
                            {
                                //重置所有列头节点
                                csvMainForm.controller.ReFlushColumns();
                            }
                            catch
                            {
                                
                            }
                        }
                        csvMainForm.controller.SerializeToIni(csvMainForm.csvConfig, csvMainForm.FullPath + "\\csv配置文件.json");
                        // 获取旧键对应的值
                        var ngValue = csvMainForm.hwindowFlowNodeUrseControl.ngDictionary[label1.Text + beginText];
                        // 获取字典的顺序
                        var ngDic = csvMainForm.hwindowFlowNodeUrseControl.ngDictionary.ToList();
                        // 删除旧的键
                        csvMainForm.csvConfig.cailpeVlues.Remove(label1.Text + beginText);
                        // 在原位置插入新键
                        bool inserted2 = false;
                        csvMainForm.hwindowFlowNodeUrseControl.ngDictionary.Clear();
                        foreach (var item in ngDic)
                        {
                            if (!inserted2 && item.Key == label1.Text + beginText)
                            {
                                // 在找到的旧键位置插入新键
                                csvMainForm.hwindowFlowNodeUrseControl.ngDictionary.Add(page.Text, ngValue);
                                inserted2 = true;
                            }
                            else
                            {
                                // 保持其他项
                                csvMainForm.hwindowFlowNodeUrseControl.ngDictionary.Add(item.Key, item.Value);
                            }
                        }
                        csvMainForm.hwindowFlowNodeUrseControl.Refresh(true);
                    }

                }

            }
            catch (Exception ex)
            {
                textBox模板.Text = beginText.Trim();
                MessageBox.Show("重命名失败:" + ex.Message);
            }
            finally
            {
                beginText = string.Empty;
                button2.Enabled = false;
                textBox模板.Enabled = false;
                button1.Enabled = true;
            }
        }
        /// <summary>
        /// 是否启用按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBox1.Tag.ToString() == "1") return;

                bool state = checkBox1.Checked;

                // 显示确认框
                DialogResult result = MessageBox.Show(
                    "请确保当前软件在非运行状态 您确定要执行此操作吗？",
                    "确认操作",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question
                );
                if (result == DialogResult.Yes)
                {
                    csvMainForm.csvConfig.ModelChange(state);
                }
                else
                {
                    checkBox1.Tag = 1;
                    checkBox1.Checked = !state;
                    checkBox1.Tag = 0;
                }
            }
            catch
            {

            }
        }
    }
}
